<?php
class Candidate_experience_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('candidate_experience.*, job_hierarchy.name AS hierarchy, job_category.name AS category,
    					job_specialty.name AS specialty, country.name AS countryname, state.name AS statename');
    	$this->db->from('candidate_experience');
    	$this->db->join('job_hierarchy', 'candidate_experience.idhierarchy = job_hierarchy.id', 'left');
    	$this->db->join('job_category', 'candidate_experience.idcategory = job_category.id', 'left');
    	$this->db->join('job_specialty', 'candidate_experience.idspecialty = job_specialty.id', 'left');
    	$this->db->join('country', 'candidate_experience.idcountry = country.id', 'left');
    	$this->db->join('state', 'candidate_experience.idstate = state.id', 'left');
    	
    	if (isset($data['idcandidate'])) {
    		$this->db->where('candidate_experience.idcandidate', $data['idcandidate']);
    	}
    	
    	if (isset($data['idexperience']) && is_numeric($data['idexperience'])) {
    		$this->db->where('candidate_experience.id', $data['idexperience']);
    	}
    	
    	if (isset($data['idlevel']) && is_numeric($data['idlevel'])) {
    		$this->db->where('candidate_experience.idlevel', $data['idlevel']);
    	}
    	
    	if (isset($data['idhierarchy']) && is_numeric($data['idhierarchy'])) {
    		$this->db->where('candidate_experience.idhierarchy', $data['idhierarchy']);
    	}
    	
    	if (isset($data['idcategory']) && is_numeric($data['idcategory'])) {
    		$this->db->where('candidate_experience.idcategory', $data['idcategory']);
    	}
    	
    	if (isset($data['idspecialty']) && is_numeric($data['idspecialty'])) {
    		$this->db->where('candidate_experience.idspecialty', $data['idspecialty']);
    	}
    	
    	if (isset($data['company']) && $data['company'] != '') {
    		$this->db->where('candidate_experience.company', $data['company']);
    	}
    	
    	if (isset($data['startdate']) && $data['startdate'] != '') {
    		$this->db->where('candidate_experience.startdate', $data['startdate']);
    	}
    	
   	 	if (isset($data['finishdate']) && $data['finishdate'] != '') {
    		$this->db->where('candidate_experience.finishdate', $data['finishdate']);
    	}
    	
    	$this->db->order_by('candidate_experience.id');
    	
    	return $this->db->get();
    }
    
	function insert($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'idhierarchy' => $data['idhierarchy'],
		   'idcategory' => $data['idcategory'],
		   'idspecialty' => $data['idspecialty'],
		   'startdate' => $data['startdate'],
		   'idcandidate' => $data['idcandidate'],
		   'title' => $data['title'],
		   'company' => $data['company'],
		   'description' => $data['description'],
		   'idnow' => $data['idnow']
		);
		
		if (isset($data['finishdate']) && $data['finishdate'] != '')
		{
			$data_insert['finishdate'] = $data['finishdate'];
		}
		
		return $this->db->insert('candidate_experience', $data_insert);
    }
    
	function update($data)
    {
    	$data_insert = array(
		   'idcountry' => $data['idcountry'],
		   'idstate' => $data['idstate'],
		   'idhierarchy' => $data['idhierarchy'],
		   'idcategory' => $data['idcategory'],
		   'idspecialty' => $data['idspecialty'],
		   'startdate' => $data['startdate'],
    	   'title' => $data['title'],
		   'company' => $data['company'],
		   'description' => $data['description'],
		   'idnow' => $data['idnow']
		);
		
    	if (isset($data['finishdate']) && $data['finishdate'] != '')
		{
			$data_insert['finishdate'] = $data['finishdate'];
		}
		
		$this->db->where('id', $data['idexperience']);
		
		return $this->db->update('candidate_experience', $data_insert);
    }
    
	function delete($data)
    {
    	$data_delete = array(
		   'id' => $data['id']
		);
		
		return $this->db->delete('candidate_experience', $data_delete);
    }
}
?>